package com.ruoyi.quartz.task;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.ruoyi.common.core.mail.Mail;
import com.ruoyi.common.easyexcel.LocalDateConverter;
import com.ruoyi.common.easyexcel.LocalDateTimeConverter;
import com.ruoyi.common.utils.DatabaseUtils;
import com.ruoyi.common.utils.EasyExcelUtil;
import com.ruoyi.common.utils.SendEmailUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.quartz.domain.StockBasic;
import com.ruoyi.quartz.mapper.StockBasicMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{

    @Value("${mail.host}")
    private String host;
    @Value("${mail.username}")
    private String userName;
    @Value("${mail.password}")
    private String passWord;

    @Resource
    private StockBasicMapper stockBasicMapper;

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String tableName) throws Exception
    {

        List<Map<String,Object>> result = stockBasicMapper.findDataToMap(tableName);

        Map<String ,Object> tmp = null;

        List<String> list = new ArrayList<>();


        tmp = result.get(0);
        for (String key:tmp.keySet())
        {
            list.add(key);
        }


        String[] head = new String[list.size()];
        for(int i=0;i<list.size();i++){
            head[i] = list.get(i);
        }

        FileOutputStream fo = new FileOutputStream("D:\\ab.xlsx");

        write(fo,head,"ab",result,head);


    }

    public void ryNoParams() throws Exception
    {
        System.out.println("执行无参方法");

        String subject ="管理员测试邮件";
        Mail mail = new Mail();
        mail.setSendFrom(userName);
        mail.setSendTo(userName);
        mail.setSubject(subject);
        mail.setContent("test");

        SendEmailUtils.send(SendEmailUtils.createSession(host,userName,passWord),mail);
    }


    //不创建对象的导出
    public void write(OutputStream out, String[] headArr, String sheetName, List<Map<String,Object>> dataList, String[] fieldArr) throws IOException {
        EasyExcel.write(out).registerConverter(new LocalDateConverter()).registerConverter(new LocalDateTimeConverter()).head(head(headArr)).sheet(sheetName).doWrite(dataList(dataList, fieldArr));
    }

    //设置表头
    private List<List<String>> head(String[] headMap) {
        List<List<String>> list = new ArrayList<List<String>>();
        for (String head : headMap) {
            List<String> headList = new ArrayList<String>();
            headList.add(head);
            list.add(headList);
        }
        return list;
    }

    //设置导出的数据内容
    private List<List<Object>> dataList(List<Map<String,Object>> dataList, String[] dataStrMap) {
        List<List<Object>> list = new ArrayList<>();
        dataList.stream().forEach(map->{
            List<Object> data = new ArrayList<Object>();
            for (int i = 0; i < dataStrMap.length; i++) {
                data.add(map.get(dataStrMap[i]));
            }
            list.add(data);
        });
        return list;
    }
}
